package ai.peanut.one.common.redis.lock;

import org.redisson.api.RLock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;
@Component
public class RedissonLockUtil {
    @Autowired
    private static DistributedLocker redissonLocker;

    public RedissonLockUtil() {
    }

    public static void setLocker(DistributedLocker locker) {
        redissonLocker = locker;
    }

    public static RLock lock(String lockKey) {
        return redissonLocker.lock(lockKey);
    }

    public static void unlock(String lockKey) {
        redissonLocker.unLock(lockKey);
    }

    public static void unlock(RLock lock) {
        redissonLocker.unLock(lock);
    }

    public static RLock lock(String lockKey, int timeout) {
        return redissonLocker.lock(lockKey, timeout);
    }

    public static RLock lock(String lockKey, TimeUnit unit, int timeout) {
        return redissonLocker.lock(lockKey, unit, timeout);
    }

    public static boolean tryLock(String lockKey, int waitTime, int leaseTime) {
        return redissonLocker.tryLock(lockKey, TimeUnit.SECONDS, waitTime, leaseTime);
    }

    public static boolean tryLock(String lockKey, TimeUnit unit, int waitTime, int leaseTime) {
        return redissonLocker.tryLock(lockKey, unit, waitTime, leaseTime);
    }
}
